package org.arnetminer.personalized.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SecuFilter extends HttpServlet implements Filter {
    private FilterConfig filterConfig;
    private String loginPage;
    private String loginAction;
    private String registerAction;
    
    /*
     * Handle the passed-in FilterConfig
     * 
     * @param	filterConfig	filter configuration parameters
     * 
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        loginPage = filterConfig.getInitParameter("loginPage");
        loginAction = filterConfig.getInitParameter("loginAction");
        registerAction = filterConfig.getInitParameter("registerAction");
    }

    /*
     * Process the request/response pair
     * 
     * @param	request		ServletRequest
     * @param	response	ServletResponse
     * @param	filterChain	FilterChain
     * 
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) {
        HttpServletRequest hreq = (HttpServletRequest)request;
        HttpServletResponse hres = (HttpServletResponse)response;
        try {
        	String url = hreq.getServletPath();
        	if ((url.endsWith(".do") && !url.equals(loginAction) && !url.equals(registerAction)) || (url.endsWith(".jsp") && !url.equals(loginPage))) {
        		HttpSession hs=hreq.getSession();
                if((hs != null) && (hs.getAttribute("account") != null)){
                    hres.setHeader("Cache-Control","no-cache");
                    hres.setHeader("Cache-Control","no-store");
                    hres.setDateHeader("Expires", 0);
                    hres.setHeader("Pragma","no-cache");
                    filterChain.doFilter(request, response);
                    return;
                }
        	}
        	else {
        		filterChain.doFilter(request, response);
                return;
        	}
            hres.sendRedirect(hreq.getContextPath() + loginPage);
        } catch (ServletException sx) {
            filterConfig.getServletContext().log(sx.getMessage());
        } catch (IOException iox) {
            filterConfig.getServletContext().log(iox.getMessage());
        }
    }

    /*
     * Clean up resources
     * 
     * @see javax.servlet.GenericServlet#destroy()
     */
    public void destroy() {
    	
    }
}
